package com.blb.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.service.*;
import com.blb.utils.LayuiPage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author txy
 * @since 2019-07-11
 */
@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private MenuService MenuService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private ICommodityService commodityService;

    @Autowired
    private IGoodstypeService goodstypeService;

    @Autowired
    private IComimgService comimgService;

    /**
     * 跳转商品维护
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model, @Param("menuid") String menuid) {
        List<Menu> list = MenuService.getBts(menuid);
        model.addAttribute("bts", list);
        return "admin/commodity/commodity_list";
    }

    /**
     * 商品管理分页
     *
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public LayuiPage page(int page, int limit, String name) {
        Page<Commodity> page2 = new Page<Commodity>(page, limit);

        IPage<Commodity> iPage = null;

        iPage = commodityService.page(page2, null);

        if (name != null && !name.equals("")) {
            iPage = commodityService.page(page2, new QueryWrapper<Commodity>().like("name", name));
        }


        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCount((int) iPage.getCurrent());
        layuiPage.setData(iPage.getRecords());

        return layuiPage;
    }

    /**
     * 跳转添加
     *
     * @param model
     * @return
     */
    @RequestMapping("showAdd")
    public String showAdd(Model model, Commodity commodity) {
        //获取所有商品分类
        List<Comtype> productTypeNameList = brandService.productTypeNameList();
        model.addAttribute("comtype", productTypeNameList);

        //获取所有商品品牌
        List<Brand> brand = brandService.getBrand();
        model.addAttribute("brands", brand);


        return "admin/commodity/commodity_add";
    }

    /**
     * 添加商品
     * 添加商品详情
     * 添加商品与类别关联表
     *
     * @param commodity
     * @param dimg
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public String add(Commodity commodity, String dimg, String comtypeName) {
        try {
            commodityService.add(commodity, dimg, comtypeName);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败" + e.getMessage();
        }
    }

    /**
     * 跳转修改
     *
     * @return
     */
    @RequestMapping("showUpdate")
    public String showupdate(Model model, Commodity commodity) {
        model.addAttribute("commodity", commodity);

        //获取所有商品分类
        List<Comtype> productTypeNameList = brandService.productTypeNameList();
        model.addAttribute("comtype", productTypeNameList);

        //获取所有商品品牌
        List<Brand> brand = brandService.getBrand();
        model.addAttribute("brands", brand);

        //获取该商品的所有分类
        List<Goodstype> goodstypeID = goodstypeService.getGoodstypeByid(commodity.getId());
        String goodstypeIDs = "";
        if (!goodstypeID.isEmpty()) {
            goodstypeIDs = JSON.toJSON(goodstypeID).toString();
        }
        model.addAttribute("goodstypeIDs", goodstypeIDs);

        //查询当前商品的详细图片
        List<Comimg> comimgs = comimgService.getComimgById(commodity.getId());
        StringBuffer comimgsBuffer = new StringBuffer();
        int i = 0;
        for (Comimg comimg : comimgs) {
            comimgsBuffer.append(comimg.getImg());
            if (i < comimgs.size() - 1) {
                comimgsBuffer.append(",");
            }
            i++;
        }
        String comimgsString = "";
        if (comimgsBuffer.length() > 0) {
            comimgsString = JSON.toJSON(comimgsBuffer).toString();
        }
        model.addAttribute("comimgsString", comimgsString);

        return "admin/commodity/commodity_update";
    }

    /**
     * 修改商品
     * 修改商品详情
     * 修改商品与类别关联表
     *
     * @param commodity
     * @param dimg
     * @param comtypeName
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public String update(Commodity commodity, @Param("dimg") String dimg, @Param("comtypeName") String comtypeName) {
        try {
            commodityService.update(commodity, dimg, comtypeName);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败" + e.getMessage();
        }

    }

    /**
     * 删除商品
     * 删除商品详情
     * 删除商品与类别关联表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public String delete(@RequestBody List<String> ids) {
        try {
            commodityService.delete(ids);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败" + e.getMessage();
        }
    }
}

